package com.test.clientsocket;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import com.zack.util.DataTransfer;

	
	public class MessageListener implements Runnable {
        private Socket socket; 
        private DataTransfer selfData;
        private String name;
        ObjectInputStream in = null;
		public MessageListener(Socket socket,String name){
			this.socket = socket;
			this.name = name;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
				while(true){
					Thread.sleep(500);
					DataTransfer serverDT = (DataTransfer) in.readObject();
					if(!name.equals(serverDT.getName())){
			    		System.out.println("服务器消息:"+serverDT.getName()+"说:"+serverDT.getContent());
			    	}
				
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally{
				try {
					socket.close();
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		}
		
		
	}

